package com.hht.checkversion;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView mListView;
    private VersionAdapter mVersionAdapter;
    private List<AppBean> mAppBeanList;
    private TextView mImportButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = findViewById(R.id.list_view);
        mImportButton = findViewById(R.id.top_app_import_tv);
        mAppBeanList = PackageUtil.getInstance().getAllAppInfo(this);
        mVersionAdapter = new VersionAdapter(this, mAppBeanList);
        mListView.setAdapter(mVersionAdapter);
        mImportButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveFileToSd();
            }
        });
    }

    //导入U盘
    private void saveFileToSd() {
        String usbPath = FileTools.getStoragePaths(this);
        if (TextUtils.isEmpty(usbPath)) {
            Toast.makeText(this, "请插入U盘", Toast.LENGTH_LONG).show();
        } else {
            try {
                ExcelUtil.writeExcel(this, mAppBeanList, usbPath, fileName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private String fileName() {
        return "HhtCheckVersion" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
}
